//
//  WSAuthorMoreViewController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSAuthorMoreViewController.h"
#import "WSAppTools.h"
#import "WSAuthorModel.h"
#import "WHAuthorMoreCell.h"
#import "WHAuthorDetailViewController.h"
#import "WSBmob.h"
#import <BmobSDK/BmobObject.h>
#import <WSProgressHUD.h>
#import "WSLoginViewController.h"

@interface WSAuthorMoreViewController ()<UITableViewDataSource,UITableViewDelegate, WHAuthorMoreCellDelegate> {
    
    WSProgressHUD *hud;
}

@property (nonatomic, strong) WSBaseTableView *myTableView;
@property (nonatomic, strong) NSMutableArray *myArray;

@end

@implementation WSAuthorMoreViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.title = self.authorTitle;
    [self createTableView];
    self.myArray = [NSMutableArray array];
    
    hud = [[WSProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    
    [self getWorking];
    
    //返回按钮
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.backgroundColor = CLEAR;
    [backButton setImage:[UIImage imageNamed:@"btn_back_n"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 20, 30);
    [backButton addTarget:self action:@selector(backButtonActionA:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
#pragma mark - 返回按钮方法
-(void)backButtonActionA:(UIButton *)btn {
    
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - create UITableView

-(void)createTableView{
    
    self.myTableView = [[WSBaseTableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
    }];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[WHAuthorMoreCell class] forCellReuseIdentifier:@"WHAuthorMoreCell"];
    self.myTableView.rowHeight = 150.f;
    self.myTableView.sectionHeaderHeight = 50.f;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - 请求数据
-(void)getWorking{
    
    NSString *name = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=hot&device=iPhone&page=1&per_page=20",self.authorName];
    
    [hud showWithString:nil maskType:WSProgressHUDMaskTypeBlack];
    [WSAppTools getDataWithURL:name block:^(id result) {
       
        NSDictionary *dic = result;
        NSArray *listArr = [dic objectForKey:@"list"];
        
        [[WSBmob shareBmob] selectRelationWithUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"uId"] tableName:@"Collect_author" block:^(id results) {
            
            NSMutableArray *selectArr = results;
            for (NSDictionary *listDic in listArr) {
                
                WSAuthorModel *authorM = [WSAuthorModel modelWithDictionary:listDic];
                authorM.objectId = nil;
                for (BmobObject *post in selectArr) {
                    if ([[post objectForKey:@"authorName"] isEqualToString:authorM.nickname]) {
                        authorM.objectId = [post objectForKey:@"objectId"];
                        break;
                    }
                }
                [self.myArray addObject:authorM];
            }
            [self.myTableView reloadData];
            [hud dismiss];
        }];
        
    } error:^(NSError *error) {
        
    }];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.myArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WHAuthorMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WHAuthorMoreCell"];
    WSAuthorModel *model = self.myArray[indexPath.row];
    cell.delegate = self;
    cell.authorM = model;
    return cell;
}

- (void)collectAuthor:(WSAuthorModel *)model {

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"1"]) {
        if (model.objectId != nil) {
            
            [self alertViewControllerWithTitle:@"取消关注？" content:nil block:^{
                
                [hud showWithString:nil];
                [[WSBmob shareBmob] deleteRelationWithObjectId:model.objectId tableName:@"Collect_author" block:^(id results) {
                    
                    if ([results isEqualToString:@"successful"]) {
                        
                        model.objectId = nil;
                    }
                    [self.myTableView reloadData];
                    [hud dismiss];
                }];
            }];
        }
        else {
            
            [hud showWithString:nil];
            [[WSBmob shareBmob] collectAuthorWithAuthorId:model.uid title:model.nickname content:model.personDescribe image:model.largeLogo block:^(id result) {
                
                if ([result isEqualToString:@"fail"] || result == nil) {
                    
                    [self alertViewControllerWithTitle:@"提示" content:@"关注失败"];
                }
                else {
                    
                    model.objectId = result;
                }
                [self.myTableView reloadData];
                [hud dismiss];
            }];
        }
    }
    else {
        
        WSLoginViewController *loginVC = [[WSLoginViewController alloc] init];
        UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        navC.navigationBarHidden = YES;
        [self presentViewController:navC animated:YES completion:nil];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WSAuthorModel *model = [self.myArray objectAtIndex:indexPath.row];
    WHAuthorDetailViewController *authorDetailVC = [[WHAuthorDetailViewController alloc] init];
    
    authorDetailVC.uid = model.uid;
    
    [self.navigationController pushViewController:authorDetailVC animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
